

<template>
  <div class="w-[350px] border border-gray-200 bg-white p-3 shadow-md rounded-lg " @click="openDrawer">
    <Handle type="target" :position="Position.Left" />
    <div class="flex flex-col gap-y-1">
      <common-title-module />
      <CommonDisplayModule title="输入" value-name="type" :valueList="data?.inoutlist" />
      <CommonDisplayModule title="输出" value-name="type" :valueList="data?.outputjsonlist" />
    </div>
    <Handle type="source" :position="Position.Right" />
  </div>
  <CodeDrawerModule ref="drawerRef" :title="node?.node?.label" :form="data" @add="handleClickAddBtn" @remove="handleClickDeleteBtn" @cancel="onCancel" />
</template>
<script setup>
import { ref } from 'vue';
import { Handle, Position, useNode } from '@vue-flow/core';
import { editNode } from '@/api/modules/flow';

import CommonTitleModule from '../modules/common-title-module.vue';
import CommonDisplayModule from '../modules/common-display-module.vue';
import CodeDrawerModule from '../modules/code-drawer-module.vue';
defineProps();
const node = useNode();

const DEFAULT_CODE = `async function main({ params }){
    // 构建输出对象
    const ret = {
        "key0": params.input + params.input, // 拼接两次入参 input 的值
        "key1": ["hello", "world"], // 输出一个数组
    };
    return ret;
}`

const data = ref({
  excepjsondata: node.node.data?.excepjsondata || {
    timeout: 60,
    retrycount: '不重试',
    excepmethod: '中断流程',
  },
  codeide: node.node.data?.codeide || DEFAULT_CODE,
  inoutlist: node.node.data?.inoutlist || [],
  outputjsonlist: node.node.data?.outputjsonlist || [],
})


const drawerRef = ref(null);
const openDrawer = () => {
  drawerRef.value?.init();
};


function handleClickAddBtn(listKey) {
  data.value[listKey].push({
    varname: '',
    vartype: 'String',
    vardesc: '',
  });
  node.node.data = {
    ...node.node.data,
  };
}

function handleClickDeleteBtn(listKey,index) {
  data.value[listKey].splice(index, 1);
  node.node.data = {
    ...node.node.data,
  };
}

const onCancel = async() => {
  await editNode({data: { id: node.id, ...data.value}})
}
</script>